package dilmaj.server;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.jdo.PersistenceManager;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

import dilmaj.client.TermJDO;
import dilmaj.client.UserJDO;
import dilmaj.client.UserService;
import dilmaj.shared.TermVO;
import dilmaj.shared.UserVO;

public class UserServiceImpl extends RemoteServiceServlet implements
		UserService {
	/*private String[] users={"Ali","Sohail","Farzin","Maryam","Mehdi","Sina"};
	private String[] passes={"Dilmaj_Ali","Dilmaj_Sohail","Dilmaj_Farzin","Dilmaj_Maryam","Dilmaj_Mehdi","Dilmaj_Sina"};*/
	
	@Override
	public UserVO login(String user, String pass) {
		// TODO Auto-generated method stub
		/*List<UserJDO> userJDOs=new ArrayList<UserJDO>();
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String result="";
		for (int i=0; i<users.length;i++) {
	            pm.makePersistent(new UserJDO(users[i], passes[i]));
		}
		
	    String query = "select from " + UserJDO.class.getName();
	    userJDOs = (List<UserJDO>) pm.newQuery(query).execute();
		return null;*/
		/*for (int i=0; i<users.length;i++) {
	    PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + UserJDO.class.getName()+" where username=="+users[i];
	    List<UserJDO> userJDOs = (List<UserJDO>) pm.newQuery(query).execute();
	    pm.deletePersistentAll(userJDOs);
		}
		return null;
	    //if (userJDOs.size()<1)
		//try {
			//pm.makePersistentAll(userJDOs);
		//}
		//userJDOs.clear();
	    /*String query = "select from " + UserJDO.class.getName();
	    userJDOs = (List<UserJDO>) pm.newQuery(query).execute();
	    
	    Iterator iterator=userJDOs.iterator();
	    int i=0;
	    while (iterator.hasNext()) {
	    	UserJDO jdo=(UserJDO)iterator.next();
			result=result.concat("User:"+jdo.getUsername()+"-Pass:"+jdo.getPassword()+"/");
	    	i++;
	    }
	    
	    return userJDOs.size()+"//"+result+":username error";*/
		
		// Proper Login
	    PersistenceManager pm = PMF.get().getPersistenceManager();
		String query = "select from " + UserJDO.class.getName()+" where username=='"+user+"'";
	    List<UserJDO> userJDOs = (List<UserJDO>)pm.newQuery(query).execute();
	    
	    UserVO result=new UserVO();
	    if (userJDOs.size()!=1) {
		    result.setUsername("username error");
	    	return result;
	    }
	   
	    UserJDO userJDO=userJDOs.get(0);
	    String password=userJDO.getPassword();
	    
	    if (password.compareTo(pass)!=0) {
	    	result.setUsername("password error");
	    	return result;
	    }

	    UserVO userVO=new UserVO(userJDO);
	    getThreadLocalRequest().getSession().setAttribute("username", userVO.getUsername());
	    getThreadLocalRequest().getSession().setAttribute("userid", userVO.getId());
		return userVO;
	}

	@Override
	public UserVO addUser(UserVO newUser) {
        UserJDO userJDO = new UserJDO(newUser);

        PersistenceManager pm = PMF.get().getPersistenceManager();
        try {
            pm.makePersistent(userJDO);
        } finally {
            pm.close();
        }
        
        return newUser;
	}

	@Override
	public UserVO getLogin() {
		String username=(String)getThreadLocalRequest().getSession().getAttribute("username");
		Long userid=(Long)getThreadLocalRequest().getSession().getAttribute("userid");
		if (username==null) 
			return null;
		
		return new UserVO(userid, username);
	}
	
	@Override
	public String logout() {
		getThreadLocalRequest().getSession().setAttribute("username", null);
		getThreadLocalRequest().getSession().setAttribute("userid", null);
		return "";
	}
}
